/*
 * Thread.cpp
 *
 *  Created on: 11.05.2010
 *      Author: stg7
 */
// Ein/Ausgabe
#include <iostream>
// Threadkrams
#include <string>
#include <pthread.h>
#include <assert.h>

class Thread {

private:
	static pthread_mutex_t globalmux;

	volatile bool m_stoprequested; // Note 5
	volatile bool m_running;

	pthread_t m_thread;

	static   pthread_mutexattr_t pthread_mutexattr_default;//pthread_mutex_t m_mutex;


	static void* start_thread(void *obj){
		reinterpret_cast<Thread *>(obj)->run();
		return NULL;
	}


protected:
	virtual void run(){

	}
public:
	Thread(): m_stoprequested(false), m_running(false){


	}
	~Thread(){

	}
	static void mutex_init(){
		pthread_mutex_init (&globalmux, NULL);
	}

	static void lock(){
		pthread_mutex_lock(&globalmux);

	}
	static void unlock(){
		pthread_mutex_unlock(&globalmux);


	}
	virtual void start(){
		assert(m_running == false);
		m_running = true;
		pthread_create(&m_thread, NULL, Thread::start_thread,this);
	}
	virtual void stop(){
		assert(m_running == true);
		m_running = false;
		m_stoprequested = true;
		pthread_exit(NULL);

	}
	virtual void join(){
		pthread_join(m_thread, 0);

	}

};


